package redtankface;

import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * EnemyInfo
 */
public class EnemyInfo {
	private final TankCoordinates mCoordinates;
	private long mTeak;
	private final String mName;
	private volatile long fireCount = 0;
	private volatile long hitCount = 0;
	private volatile double speed = 0;
	private volatile double handle = 0;
	private static final double SPEED_WEIGHT = 0.5;

	public EnemyInfo(ScannedRobotEvent aEvent, RedTankFace aRobot, long aTeak, String aName) {
		mCoordinates = new TankCoordinates();
		mName = aName;
		updateData(aEvent, aRobot, aTeak);
	}

	public void updateData(ScannedRobotEvent aEvent, RedTankFace aRobot, long aTeak) {
		mCoordinates.setData(aEvent, aRobot);
		mTeak = aTeak;
		speed = aEvent.getVelocity();
		handle = aEvent.getHeadingRadians();
	}

	public TankCoordinates getCoord(long aTeak) {
		double newX = mCoordinates.mX + (speed * Math.sin(handle) * (aTeak - mTeak) * SPEED_WEIGHT) * getAccuracy() * 5 / Math.max(5, Math.min(1, aTeak - mTeak));
		double newY = mCoordinates.mY + (speed * Math.cos(handle) * (aTeak - mTeak) * SPEED_WEIGHT) * getAccuracy() * 5 / Math.max(5, Math.min(1, aTeak - mTeak));
		TankCoordinates ret = new TankCoordinates();
		ret.setData(newX, newY);
		return ret;
	}

	public String getName() {
		return mName;
	}

	public long getTime() {
		return mTeak;
	}

	public void recordFire() {
		fireCount++;
	}

	public void recordHit() {
		hitCount++;
	}

	public long getFireCount() {
		return fireCount;
	}

	public long getHitCount() {
		return hitCount;
	}

	public double getAccuracy() {
		return Math.max(hitCount, 1) / Math.max(fireCount, 1);
	}
}
